home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Toolbox / Visual Basic Toolbox (P.I.E.)(1996).ISO / graphics / bitvctr / bitvectr.cls < prev    next >
Encoding:
Text File  |  1995-12-04  |  5.3 KB  |  251 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "BitVector"
  6. Attribute VB_Creatable = True
  7. Attribute VB_Exposed = True
  8. '
  9. ' BitVector Class
  10. ' Copyright ⌐ 1995-1996 by Gregg S. Irwin. All Rights Reserved.
  11. '
  12.  
  13. Option Explicit
  14. DefInt A-Z
  15.  
  16. Const CLASS_NAME = "BitVector"
  17. Const CLASS_VERSION = "100"
  18.  
  19.  
  20. ' * PROPERTIES *
  21. ' .NumElements
  22.  
  23. ' * METHODS    *
  24. ' .ClearAll
  25. ' .ClearBit (BitIndex)
  26. ' .GetBit   (BitIndex)
  27. ' .IsBitSet (BitIndex)
  28. ' .SetAll
  29. ' .SetBit   (BitIndex)
  30. ' .Toggle   (BitIndex)
  31.  
  32. ' * ERRORS     *
  33. ' Subscript out of range
  34.  
  35.  
  36. Const vbErrSubscriptOutOfRange = 9
  37.  
  38. Const BITS_PER_ELEMENT = 8
  39.  
  40.  
  41. Private mBits()      As Byte
  42. Private mNumElements As Long
  43.  
  44.  
  45. '-- The following code:
  46. '
  47. '      ArrayIdx = Index \ BITS_PER_ELEMENT
  48. '      Bit = Index Mod BITS_PER_ELEMENT
  49. '
  50. '   appears in a few procedures and could/should be
  51. '   broken out into one or two procedures itself. I
  52. '   just haven't been able to come up with a good,
  53. '   clean syntax that I like yet.
  54.  
  55.  
  56. '------------------------------------------------------
  57. '-- CLASS EVENTS
  58. '------------------------------------------------------
  59.  
  60. Private Sub Class_Initialize()
  61.  
  62. End Sub
  63.  
  64.  
  65. Private Sub Class_Terminate()
  66.     
  67.     Erase mBits
  68.  
  69. End Sub
  70.  
  71.  
  72. '------------------------------------------------------
  73. '-- PROPERTIES
  74. '------------------------------------------------------
  75.  
  76. Public Property Let NumElements(NewValue As Long)
  77.     
  78.     '-- TBD  Trap for bad values
  79.     
  80.     mNumElements = NewValue
  81.     ReDim Preserve mBits(mNumElements \ BITS_PER_ELEMENT)
  82.     'Debug.Print UBound(mBits)
  83.  
  84. End Property
  85.  
  86.  
  87. Public Property Get NumElements() As Long
  88.     
  89.     NumElements = mNumElements
  90.  
  91. End Property
  92.  
  93.  
  94. '------------------------------------------------------
  95. '-- METHODS
  96. '------------------------------------------------------
  97.  
  98. Public Sub ClearAll()
  99.     Dim i As Long
  100.     
  101.     '-- Set bit values in BITS_PER_ELEMENT chunks for speed
  102.     For i = LBound(mBits) To UBound(mBits)
  103.         mBits(i) = &H0
  104.     Next i
  105.     
  106. End Sub
  107.  
  108. Public Sub ClearBit(Index As Long)
  109. '-- Set Bit(Index) value to 0
  110.     Dim ArrayIdx As Long
  111.     Dim Bit      As Long
  112.     
  113.     Call ValidateIndex(Index)
  114.     
  115.     ArrayIdx = Index \ BITS_PER_ELEMENT
  116.     Bit = Index Mod BITS_PER_ELEMENT
  117.     'Debug.Print "Clearing ArrayIdx:"; ArrayIdx, " Bit:"; Bit
  118.     mBits(ArrayIdx) = mBits(ArrayIdx) And (Not (2 ^ Bit))
  119.     
  120. End Sub
  121.  
  122.  
  123. Public Function GetBit(Index As Long) As Integer
  124. '-- Returns 0 or 1
  125.     
  126.     Call ValidateIndex(Index)
  127.     
  128.     If IsBitSet(Index) Then
  129.         GetBit = 1
  130.     Else
  131.         GetBit = 0
  132.     End If
  133.     
  134. End Function
  135.  
  136.  
  137. Public Function IsBitSet(Index As Long) As Boolean
  138.     Dim ArrayIdx As Long
  139.     Dim Bit      As Long
  140.     
  141.     Call ValidateIndex(Index)
  142.     
  143.     ArrayIdx = Index \ BITS_PER_ELEMENT
  144.     Bit = Index Mod BITS_PER_ELEMENT
  145.     'Debug.Print "Testing ArrayIdx:"; ArrayIdx, " Bit:"; Bit
  146.     If mBits(ArrayIdx) And 2 ^ Bit Then
  147.         IsBitSet = True
  148.     Else
  149.         IsBitSet = False
  150.     End If
  151.  
  152. End Function
  153.  
  154.  
  155. Public Sub SetAll()
  156.     Dim i As Long
  157.     
  158.     '-- Set bit values in BITS_PER_ELEMENT chunks for speed
  159.     For i = LBound(mBits) To UBound(mBits)
  160.         mBits(i) = &HFF
  161.     Next i
  162.     
  163. End Sub
  164.  
  165.  
  166. Public Sub SetBit(Index As Long)
  167. '-- Set Bit(Index) value to 1
  168.     Dim ArrayIdx As Long
  169.     Dim Bit      As Long
  170.     
  171.     Call ValidateIndex(Index)
  172.     
  173.     ArrayIdx = Index \ BITS_PER_ELEMENT
  174.     Bit = Index Mod BITS_PER_ELEMENT
  175.     'Debug.Print "Setting ArrayIdx:"; ArrayIdx, " Bit:"; Bit
  176.     mBits(ArrayIdx) = mBits(ArrayIdx) Or 2 ^ Bit
  177.  
  178. End Sub
  179.  
  180.  
  181. Public Sub ToggleBit(Index As Long)
  182. '-- Toggle the value of Bit(Index)
  183.     
  184.     Call ValidateIndex(Index)
  185.     
  186.     If IsBitSet(Index) Then
  187.         Call ClearBit(Index)
  188.     Else
  189.         Call SetBit(Index)
  190.     End If
  191.     
  192. End Sub
  193.  
  194.  
  195. '------------------------------------------------------
  196. '-- INTERNAL SUPPORT
  197. '------------------------------------------------------
  198.  
  199. ''!! This is an unused (and untested) procedure. It's just
  200. ''   here to remind me that we can get the exponentiation
  201. ''   out of the inline code and do table lookups instead.
  202. 'Private Sub InitBitValueTable(BitValueTable() As Long)
  203. '    Dim i As Integer
  204. '
  205. '    For i = 1 To BITS_PER_ELEMENT
  206. '        BitValueTable(i) = 2 ^ i
  207. '    Next i
  208. '
  209. 'End Sub
  210.  
  211.  
  212. Private Sub ValidateIndex(Index As Long)
  213.     
  214.     '-- Our bounds checking code is aware that this is
  215.     '   a 0 based array of bits.
  216.     If (Index < 0) Or (Index > (mNumElements - 1)) Then
  217.         RaiseError vbErrSubscriptOutOfRange
  218.     End If
  219.  
  220. End Sub
  221.  
  222.  
  223. '------------------------------------------------------
  224. '-- ERRORS
  225. '------------------------------------------------------
  226.  
  227. ' .GetErrorDesc
  228. Private Function GetErrorDesc(ErrCode As Long) As String
  229.     Dim Desc As String
  230.     
  231.     Select Case ErrCode
  232.         Case vbErrSubscriptOutOfRange
  233.             Desc = "Subscript out of Range"
  234.         Case Else
  235.             Desc = "Unknown error"
  236.     End Select
  237.     
  238.     GetErrorDesc = Desc
  239.     
  240. End Function
  241.  
  242.  
  243. ' .RaiseError
  244. Private Sub RaiseError(ErrCode As Long)
  245.         
  246.     Err.Raise Number:=vbObjectError + ErrCode, _
  247.               Source:=CLASS_NAME & " " & CLASS_VERSION, _
  248.               Description:=GetErrorDesc(ErrCode)
  249.  
  250. End Sub
  251.